<template>
	<view>
		<!-- #ifdef APP-PLUS -->
		<view>
			<uni-nav-bar title="订单管理" :statusBar='true' :fixed="true"></uni-nav-bar>
		</view>
		<!-- #endif -->
		<view class="order">
			<view class="order-title">
				<view @click="onClickSelectIndex(0)" :class="{ 'order-title-item-active': selectIndex === 0 }" class="order-title-item">今日订单</view>
				<view @click="onClickSelectIndex(1)" :class="{ 'order-title-item-active': selectIndex === 1 }" class="order-title-item">过去一周</view>
				<view @click="onClickSelectIndex(2)" :class="{ 'order-title-item-active': selectIndex === 2 }" class="order-title-item">全部订单</view>
			</view>
			<!-- #ifdef APP-PLUS -->
			<scroll-view @scrolltolower="todayMore" :style="{ 'display': selectIndex === 0 ? 'flex' : 'none', 'height': appListHeight + 'px' }" :scroll-with-animation='true' v-show="selectIndex === 0" scroll-y class="order-list">
			<!-- #endif -->
			<!-- #ifndef APP-PLUS -->
			<scroll-view @scrolltolower="todayMore" :style="{ 'display': selectIndex === 0 ? 'flex' : 'none', 'height': h5ListHeight + 'px' }" :scroll-with-animation='true' v-show="selectIndex === 0" scroll-y class="order-list">
			<!-- #endif -->
				<view class="order-list-item" v-for="item of pageData_toady" :key="item.id">
					<text class="order-list-item-name">商家名称：{{ item.storeName }}</text>
					<view class="order-list-item-price">
						<text class="order-list-item-price-left">实付：¥{{ item.payAmount }}</text>
						<text class="order-list-item-price-left">节省：¥{{ item.discountAmount }}</text>
					</view>
					<view class="order-list-item-line"></view>
					<view class="order-list-item-ddh">订单号：{{ item.id }}</view>
					<view class="order-list-item-ddh">下单时间：{{ item.createdAt }}</view>
					<view class="order-list-item-ddh">小票金额：{{ item.price }}元</view>
					<view class="order-list-item-ddh">折扣率：{{ item.rate }}折</view>
				</view>
			</scroll-view>
			<!-- #ifdef APP-PLUS -->
			<scroll-view @scrolltolower="weekMore" :style="{ 'display': selectIndex === 1 ? 'flex' : 'none', 'height': appListHeight + 'px' }" :scroll-with-animation='true' v-show="selectIndex === 1" scroll-y class="order-list">
			<!-- #endif -->
			<!-- #ifndef APP-PLUS -->
			<scroll-view @scrolltolower="weekMore" :style="{ 'display': selectIndex === 1 ? 'flex' : 'none', 'height': h5ListHeight + 'px' }" :scroll-with-animation='true' v-show="selectIndex === 1" scroll-y class="order-list">
			<!-- #endif -->
				<view class="order-list-item" v-for="item of pageData_week" :key="item.id">
					<text class="order-list-item-name">商家名称：{{ item.storeName }}</text>
					<view class="order-list-item-price">
						<text class="order-list-item-price-left">实付：¥{{ item.payAmount }}</text>
						<text class="order-list-item-price-left">节省：¥{{ item.discountAmount }}</text>
					</view>
					<view class="order-list-item-line"></view>
					<view class="order-list-item-ddh">订单号：{{ item.id }}</view>
					<view class="order-list-item-ddh">下单时间：{{ item.createdAt }}</view>
					<view class="order-list-item-ddh">小票金额：{{ item.price }}元</view>
					<view class="order-list-item-ddh">折扣率：{{ item.rate }}折</view>
				</view>
			</scroll-view>
			<!-- #ifdef APP-PLUS -->
			<scroll-view @scrolltolower="totalMore" :style="{ 'display': selectIndex === 2 ? 'flex' : 'none', 'height': appListHeight + 'px' }" :scroll-with-animation='true' v-show="selectIndex === 2" scroll-y class="order-list">
			<!-- #endif -->
			<!-- #ifndef APP-PLUS -->
			<scroll-view @scrolltolower="totalMore" :style="{ 'display': selectIndex === 2 ? 'flex' : 'none', 'height': h5ListHeight + 'px' }" :scroll-with-animation='true' v-show="selectIndex === 2" scroll-y class="order-list">
			<!-- #endif -->
				<view class="order-list-item" v-for="item of pageData_total" :key="item.id">
					<text class="order-list-item-name">商家名称：{{ item.storeName }}</text>
					<view class="order-list-item-price">
						<text class="order-list-item-price-left">实付：¥{{ item.payAmount }}</text>
						<text class="order-list-item-price-left">节省：¥{{ item.discountAmount }}</text>
					</view>
					<view class="order-list-item-line"></view>
					<view class="order-list-item-ddh">订单号：{{ item.id }}</view>
					<view class="order-list-item-ddh">下单时间：{{ item.createdAt }}</view>
					<view class="order-list-item-ddh">小票金额：{{ item.price }}元</view>
					<view class="order-list-item-ddh">折扣率：{{ item.rate }}折</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import { config } from '@/utils/config.js'
	import uniNavBar from '@/components/uni-nav-bar/uni-nav-bar.vue'
	export default {
		components: {
			uniNavBar
		},
		data() {
			return {
				selectIndex: 0,
				h5ListHeight: 0,
				appListHeight: 0,
				// 今天的
				pageData_toady: [],
				page_today: 1,
				size_today: 10,
				total_today: 0,
				totalPage_today: 0,
				// 周的
				pageData_week: [],
				page_week: 1,
				size_week: 10,
				total_week: 0,
				totalPage_week: 0,
				// 总
				pageData_total: [],
				page_total: 1,
				size_total: 10,
				total_total: 0,
				totalPage_total: 0,
			}
		},
		onLoad() {
			const token = uni.getStorageSync('h_token')
			if (!token) {
				uni.showToast({
					icon: 'none',
					title: '尚未登录, 请先登录'
				})
				setTimeout(() => {
					uni.removeStorageSync('h_role')
					uni.removeStorageSync('h_memberId')
					uni.removeStorageSync('h_token')
					uni.removeStorageSync('h_openId')
					uni.reLaunch({
						url: '../verificationcodelogin/verificationcodelogin'
					})
				}, 777)
				return false
			}
			this.getPageData_today()
			this.getPageData_week()
			this.getPageData_total()
		},
		// 下拉刷新
		onPullDownRefresh() {
			if (this.selectIndex === 0) {
				this.pageData_toady = []
				this.page_today = 1
				this.size_today = 10
				this.total_today = 0
				this.totalPage_today = 0
				this.getPageData_today()
			} else if (this.selectIndex === 1) {
				this.pageData_week = []
				this.page_week = 1
				this.size_week = 10
				this.total_week = 0
				this.totalPage_week = 0
				this.getPageData_week()
			} else if (this.selectIndex === 2) {
				this.pageData_total = []
				this.page_total = 1
				this.size_total = 10
				this.total_total = 0
				this.totalPage_total = 0
				this.getPageData_total()
			}
		},
		onReady() {
			const that = this
			uni.getSystemInfo({
				success: ((res) => {
					let info = uni.createSelectorQuery().select(".order-title");
					info.boundingClientRect(function(data) {
						that.h5ListHeight = res.windowHeight - data.height // 小程序和H5是可以这么设置，但是到了自定义头部的时候，是不允许这么设置的
						that.appListHeight = res.windowHeight - res.statusBarHeight - 44 - data.height
					}).exec()
				})
			})
		},
		methods: {
			pageTotal(rowCount, pageSize) {
				if (rowCount === null || rowCount === "") {
					return 0;
				} else {
					if ((pageSize !== 0) && (rowCount % pageSize === 0)) {
						return parseInt(rowCount / pageSize)
					}
					if ((pageSize !== 0) && (rowCount % pageSize !== 0)) {
						return parseInt(rowCount / pageSize) + 1
					}
				}
			},
			getPageData_today() {
				const page =  this.page_today
				const size =  this.size_today
				uni.request({
					url: `${config.api_base_url}merchant/orders/findTodayOrders/${page}/${size}`,
					header: {
						AUTH: 'ROBOT ' + uni.getStorageSync('h_token')
					},
					success: ((res) => {
						let { status, data } = res.data
						if (status.flag === true && data && data.rows) {
							this.total_today = data.total
							this.totalPage_today = this.pageTotal(this.total_today, this.size_today)
							this.pageData_toady = this.pageData_toady.concat(data.rows)
						} else {
							this.total_today = 0
							this.totalPage_today = 0
							this.pageData_toady = []
						}
					}),
					fail: (() => {
						uni.showToast({
							icon: 'none',
							title: '请求失败, 网络异常'
						})
					}),
					complete: (() => {
						uni.stopPullDownRefresh()
					})
				})
			},
			getPageData_week() {
				const page =  this.page_week
				const size =  this.size_week
				uni.request({
					url: `${config.api_base_url}merchant/orders/findWeekOrders/${page}/${size}`,
					header: {
						AUTH: 'ROBOT ' + uni.getStorageSync('h_token')
					},
					success: ((res) => {
						let { status, data } = res.data
						if (status.flag === true && data && data.rows) {
							this.total_week = data.total
							this.totalPage_week = this.pageTotal(this.total_week, this.size_week)
							this.pageData_week = this.pageData_week.concat(data.rows)
						} else {
							this.total_week = 0
							this.totalPage_week = 0
							this.pageData_week = []
						}
					}),
					fail: (() => {
						uni.showToast({
							icon: 'none',
							title: '请求失败, 网络异常'
						})
					}),
					complete: (() => {
						uni.stopPullDownRefresh()
					})
				})
			},
			getPageData_total() {
				const page =  this.page_total
				const size =  this.size_total
				uni.request({
					url: `${config.api_base_url}merchant/orders/findTotalOrders/${page}/${size}`,
					header: {
						AUTH: 'ROBOT ' + uni.getStorageSync('h_token')
					},
					success: ((res) => {
						let { status, data } = res.data
						if (status.flag === true && data && data.rows) {
							this.total_total = data.total
							this.totalPage_total = this.pageTotal(this.total_total, this.size_total)
							this.pageData_total = this.pageData_total.concat(data.rows)
						} else {
							this.total_total = 0
							this.totalPage_total = 0
							this.pageData_total = []
						}
					}),
					fail: (() => {
						uni.showToast({
							icon: 'none',
							title: '请求失败, 网络异常'
						})
					}),
					complete: (() => {
						uni.stopPullDownRefresh()
					})
				})
			},
			todayMore() {
				const page1 = this.page_today + 1;
				const totalPage = this.totalPage_today
				if (page1 <= totalPage) {
					this.page_today = page1
					this.getPageData_today()
				} else {
					uni.showToast({
						title: '已经到底部了',
						icon: 'none'
					})
				}
			},
			weekMore() {
				const page1 = this.page_week + 1;
				const totalPage = this.totalPage_week
				if (page1 <= totalPage) {
					this.page_week = page1
					this.getPageData_week()
				} else {
					uni.showToast({
						title: '已经到底部了',
						icon: 'none'
					})
				}
			},
			totalMore() {
				const page1 = this.pageData_total + 1;
				const totalPage = this.totalPage_total
				if (page1 <= totalPage) {
					this.pageData_total = page1
					this.getPageData_total()
				} else {
					uni.showToast({
						title: '已经到底部了',
						icon: 'none'
					})
				}
			},
			onClickSelectIndex(index) {
				this.selectIndex = index
			}
		}
	}
</script>
<style>
	page {
		background-color: #F6F5F8;
	}
</style>
<style scoped>
	.order {
		width: 100%;
	}

	.order-title {
		width: 100%;
		height: 64rpx;
		display: flex;
		align-items: center;
		box-sizing: border-box;
		padding-left: 14rpx;
		padding-right: 16rpx;
		background-color: #fff;
		box-sizing: border-box;
		border-top: 1px solid #F6F5F8;
	}

	.order-title-item {
		font-size: 28rpx;
		font-weight: 600;
		width: 33.33%;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.order-title-item-active {
		color: #F02230;
		position: relative;
	}

	.order-title-item-active::after {
		position: absolute;
		left: 50%;
		margin-left: -31rpx;
		bottom: -8rpx;
		content: ' ';
		width: 62rpx;
		height: 4rpx;
		background-color: #F02230;
	}

	.order-list {
		width: 100%;
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		padding-top: 20rpx;
		padding-left: 14rpx;
		padding-right: 16rpx;
	}

	.order-list-item {
		width: 100%;
		background-color: #fff;
		border-radius: 12rpx;
		box-sizing: border-box;
		padding-top: 24rpx;
		padding-right: 28rpx;
		padding-bottom: 22rpx;
		padding-left: 36rpx;
		display: flex;
		flex-direction: column;
	}

	.order-list-item-name {
		font-size: 28rpx;
		line-height: 40rpx;
		font-weight: 600;
	}

	.order-list-item-price {
		margin-top: 12rpx;
		display: flex;
		align-items: auto;
	}

	.order-list-item-price-left {
		font-size: 36rpx;
		font-weight: 400;
		line-height: 50rpx;
		margin-right: 70rpx;
		color: rgba(0, 0, 0, 0.85);
	}

	.order-list-item-line {
		margin-top: 12rpx;
		width: 100%;
		height: 2rpx;
		background-color: #EEEEEE;
		margin-bottom: 10rpx;
	}

	.order-list-item-ddh {
		font-size: 28rpx;
		font-weight: 400;
		color: #6D7278;
		line-height: 40rpx;
		margin-bottom: 6rpx;
	}

	.order-list-item-ddh:last-child {
		margin-bottom: 0;
	}
</style>
